package com.example.controller;

import com.example.domain.Student;
import com.example.service.StudentService;
import com.example.service.impl.StudentServiceImpl2;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: sms
 * @description:
 * @author: zhanghz001
 * @create: 2020-11-08 20:10
 **/
// @WebServlet("/students/*")
public class StudentController extends HttpServlet {
    // private StudentService studentService = new StudentServiceImpl();
    private StudentService studentService = new StudentServiceImpl2();
    //method=findAllStudent

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("findAllStudent".equals(method)) {
            findAllStudent(req, resp);
        } else if ("updateStudent".equals(method)) {
            updateStudent(req, resp);
        } else if ("saveStudent".equals(method)) {
            saveStudent(req, resp);
        } else if ("toAdd".equals(method)) {
            req.getRequestDispatcher("/html/student/add.jsp").forward(req, resp);
        } else if ("findStudentById".equals(method)) {
            findStudentById(req, resp);
        } else if ("deleteStudent".equals(method)) {
            deleteStudent(req, resp);
        } else {
            resp.sendRedirect(req.getContextPath() + "/error.jsp");
        }

    }

    private void deleteStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        studentService.delete(Integer.parseInt(sid));
        findAllStudent(req, resp);
    }

    private void findStudentById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        Student student = findById(Integer.parseInt(sid));
        req.setAttribute("student", student);
        req.getRequestDispatcher("/html/student/edit.jsp").forward(req, resp);
    }

    private void saveStudent(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Student student = new Student();
            ConvertUtils.register(new DateLocaleConverter(), Date.class);
            BeanUtils.populate(student, req.getParameterMap());
            int insRow = insert(student);
            findAllStudent(req, resp);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

    private void updateStudent(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Map<String, String[]> map = req.getParameterMap();
            Student student = new Student();
            ConvertUtils.register(new DateLocaleConverter(), Date.class);
            BeanUtils.populate(student, map);
            //日期转型
            int updRow = update(student);
            findAllStudent(req, resp);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

    private void findAllStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Student> list = findAll();
        // users
        req.setAttribute("students", list);
        req.getRequestDispatcher("/html/student/list.jsp").forward(req, resp);

    }

    private void batchAdd() {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    public List<Student> findAll() {
        List<Student> studnentList = studentService.findAll();
        return studnentList;
        // for (Student student : studnentList) {
        //     System.out.println(student);
        // }
    }

    public Student findById(Integer sid) {
        Student student = studentService.findById(sid);
        return student;
    }

    public int insert(Student student) {
        int i = studentService.insert(student);
        return i;
    }

    public int update(Student stu) {
        int i = studentService.update(stu);
        return i;
    }

    public void delete() {
        int i = studentService.delete(15);
        System.out.println(i);
    }
}
